<?php



// +----------------------------------------------------------------------

// | OneThink [ WE CAN DO IT JUST THINK IT ]

// +----------------------------------------------------------------------

// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.

// +----------------------------------------------------------------------

// | Author: 麦当苗儿 <zuojiazi@vip.qq.com> <http://www.zjzit.cn>

// +----------------------------------------------------------------------



namespace Home\Controller;



use Think\Controller;



/**
 * 会员控制器
 */

class UserMemberController extends HomeController {



    /**
     * [_initialize] 控制器初始化调用
     * @author qlj
     */

    protected function _initialize() {

        //调用课时自动确认方法

        A('CourseRecord')->autoConfirm();

        D('Mycourse')->autoCancelTime();
/* echo '<pre>';
print_r($_SESSION);
print_r($_COOKIE);
die('aaa');
echo '</pre>'; */
    }



    /**
     * [verify description]验证码
     * @return [type] [description]
     */

    public function verify() {

        $Verify = new \Think\Verify();

        $Verify->fontSize = 30;

        $Verify->length = 3;

        $Verify->imageH = 60;

        $Verify->useNoise = false;

        $Verify->entry(1);

    }



    /**
     * [register description]注册的方法
     * @return [type] [description]
     */

    public function register() {
        if (IS_POST) {

            $post = I('post.');
            if (empty($post['check'])) {

                $this->error('没同意，不能提交');

                die;

            }

            //正则验证手机号码

            $data = mobile($post['mobile']);

            $verify = session('verify');
            //hjl:临时屏蔽
            if ($verify != $post['con']) {

                $this->error('验证码错误');

                die;

            }

            if (empty($post['mobile'])) {

                $this->error('手机号不能为空');

            } else if (empty($post['password'])) {

                $this->error('密码不能为空');

            }

            $data = D('UserMember')->showMobile($post['mobile']);

            if ($data) {

                $this->error('此手机号已注册');

                die;

            }

            if ($post['password'] != $post['topassword']) {

                $this->error('你俩次输入的密码不一致');

                die;

            }

            $data = D('UserMember')->addUserMember($post);

            if ($data) {

				
				/* lkl注册成功后自动登录start */
				
				session('uid', $data);

                //更改登录后的状态
				session('is_login', 0);
                D('UserMember')->updateLogin('mobile', $post['mobile'], 1);

                if (empty($data['openid'])) {

                    D('UserMember')->updateOpenid(session('uid'));

                }

                $user = D('UserMember')->showUser(session('uid'));

                //记住密码
				setcookie('username', $user['username'], time() + 3600 * 24 * 365);

				setcookie('password', $post['password'], time() + 3600 * 24 * 365);
				
				setcookie('remember', 1, time() + 3600 * 24 * 365);

                $this->success('注册成功，系统为您自动登陆。', U('Index/Index'));
				/* end */

            } else {

                $this->error('注册失败', U('UserMember/register'));

            }

        } else {

            $this->assign('identity', getIteam('identity'));

            $this->assign('constell', getIteam('constell'));

            $this->display();

        }

    }



    /**
     * [login description]登录的方法
     * @return [type] [description]
     */

    public function login() {

        // 判断openid有的话直接走，没有去获取

        if (empty(session('openid'))) {
			
			//lkl解决没有session但是有uid无限跳转
			if(!empty(session('uid'))){
				$this->logout();
				die;
			}
            $this->codes();  

        }

        if (IS_POST) {

            $post = I('post.');
            
            if(empty($post['mobile'])){
                $this->error('没填写手机号不能提交');die;
            }
            if(empty($post['password'])){
                $this->error('没填写密码不能提交');die;
            }
//            var_dump($post);exit();

            $data = D('UserMember')->showOne($post);

            if ($data == NULL) {

                $this->error('用户名不存在');

                die;

            }

            //检查是否已经登录

            D('UserMember')->is_login($post['mobile']);

            $data = D('UserMember')->contrastPwd($post);

            if ($data == NULL) {

                $this->error('密码不正确');

                die;

            } else {

                session('uid', $data['id']);

                //更改登录后的状态

                D('UserMember')->updateLogin('mobile', $post['mobile'], 1);
				
                if (empty($data['openid'])) {

                    D('UserMember')->updateOpenid(session('uid'));

                }

                $user = D('UserMember')->showUser(session('uid'));

                //记住密码

                if (I('post.remember') == 1) {

                    setcookie('username', $user['username'], time() + 3600 * 24 * 365);

                    setcookie('password', $post['password'], time() + 3600 * 24 * 365);

                    setcookie('remember', $post['remember'], time() + 3600 * 24 * 365);

                }else{

                    setcookie('username', $user['username'], time() + 3600 * 24 * 365);

                    setcookie('password', $post['password'], time() + 3600 * 24 * 365);

                    setcookie('remember', $post['remember'], time() + 3600 * 24 * 365);

                }
				
                //判断是否是同一天登录

                if (date('ymd', $user['last_login_time']) != date('ymd', time())) {

                    //如果不是，更新登录时间，查看、自荐次数

                    $update['id'] = session('uid');

                    $update['last_login_time'] = time();

                    $update['checked'] = 0;

                    $update['messages'] = 0;

                    D('UserMember')->updatePost($update);

                }

                //判断来源页

                $re_url = session('re_url');
                if (!empty($re_url)) {

                    $data['url'] = $re_url;
                    // dump($data['url']);die;

                    $data['info'] = '登录成功';
                    if($data['url'] == "http://www.quanminjiaoxue.com/index.php?s=/Home/UserMember/register.html"){
                        $this->success('Index/index', U('Index/index'));
                    }
//                                        $this ->success('Index/index',$re_url);

                    $this->ajaxReturn($data);

                } else {

                    $this->success('Index/index', U('Index/index'));

                }

            }

        } else {

            //判断是否登录

            // if (session('?uid')) {

            //  $re_url = empty(session('re_url')) ? U('Index/index') : session('re_url');

            //  header("Location:".$re_url);die;

            // } else {

            //记录来源页
            // dump($_SERVER);die;
            session('re_url', $_SERVER['HTTP_REFERER']);
            $this->display();

            // }

        }

    }



    //获取code  hjl 临时注释微信功能

    public function codes() {

         $appid = "wx24af86636d874b65";

         $redirect_uri = urlencode("http://www.quanminjiaoxue.com/index.php?s=/Home/UserMember/wx_openid");

         $wxurl = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" . $appid . "&redirect_uri=" . $redirect_uri . "&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect";

         header("Location:$wxurl");

    }



    //获取openid

    public function wx_openid() {

        $appid = "wx24af86636d874b65";

      

        $secret = "cf8e1c33e151589634b9f9d3a1c96c26";

        $wxinfo = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=" . $appid . "&secret=" . $secret . "&code=" . $_GET['code'] . "&grant_type=authorization_code";

        $info = json_decode(file_get_contents($wxinfo), true);

        session('openid', $info['openid']);

        // session('codes',$info['openid']);

        $url = "http://www.quanminjiaoxue.com/index.php?s=/Home/UserMember/login";

        header("Location:$url");

    }



    /**
     * 退出登录
     * @author qlj
     */

    public function logout() {

        D('UserMember')->updateLogin('id', session('uid'), 0);

        session(null);

        //检查是否成功

        if (!session('?uid')) {

            setcookie('username', $user['username'], time() - 3600 * 24 * 365);

            setcookie('password', $post['password'], time() - 3600 * 24 * 365);

            setcookie('remember', $post['remember'], time() - 3600 * 24 * 365);

            //跳转到列表

            header('Location:' . U('UserMember/userList'));

        } else {

            header('Location:' . U('Index/index'));

        }

    }



    public function personal() {

        $id = $_SESSION['onethink_home']['uid'];

        if (IS_POST) {


        } else {

            $this->assign('identity', getIteam('identity'));

            $this->assign('constell', getIteam('constell'));

            $data = D('UserMember')->showUser($id);

            $areas = D('Area')->getAreas();

            $towArea = D('UserMember')->towArea($data['area_id']);

            $this->assign('towArea', $towArea);

            $this->assign('data', $data);

            $this->assign('areas', $areas);

            $this->display();

        }

    }



    public function usersave() {

        $post = I('post.');

        // if(){

            

        // }

        $logo = $_FILES;

        $arr['rootPath'] = "./Uploads/Picture/";

        $logo['logo']['is_thumb'] = true;

        $file = D('Picture')->upload_thumb($logo, '', '', '', 2);

        if ($file) {

            $post['logo'] = $file['logo']['id'];

            $data = D('UserMember')->updatePost($post);

            if ($data) {

                $this->success('修改成功！', U('Index/index'));

                // $this->ajaxReturn($data);

            } else {

                $this->error('修改失败');

            }

        } else {

            $data = D('UserMember')->updatePost($post);

            if ($data) {

                $this->success('修改成功！', U('Index/index'));

            } else {

                $this->error('修改失败');

            }

        }

    }



    /**
     * 想教想学列表
     * @param  int $teaching [教2，学1]
     * @param  int $label    [课程标签]
     * @param  int $sex      [性别]
     * @param  int $age      [年龄]
     * @param  int $identity [身份]
     * @param  int $area_pid [省id]
     * @param  int $area_cid [市区id]
     * @param  int $star     [评价排序]
     * @param  int $addtime  [发布时间排序]
     * @param  int $salary   [最低课程价格排序]
     * @param  str $keywords [搜索关键词]
     * @author qlj
     */

    public function userList($teaching = '', $label = '', $sex = '', $age = '', $identity = '', $area_pid = '', $area_cid = '', $star = '', $addtime = '', $salary = '', $keywords = '', $limit = 0,$limitrow = 10) {

        if (IS_AJAX) {
            $teaching = $_POST['teaching'];

            $label = $_POST['label'];

            $sex = $_POST['sex'];

            $age = $_POST['age'];

            $identity = $_POST['identity'];

            $area_pid = $_POST['area_pid'];

            $area_cid = $_POST['area_cid'];

            $star = $_POST['star'];

            $addtime = $_POST['addtime'];

            $salary = $_POST['salary'];

            $keywords = $_POST['keywords'];

            $limit = $_POST['limit'];

                //地区id

           if (!empty($area_pid) && empty($area_cid)) {

               //如果只有父id，查询所有子id

               $areadata = D('Area')->find($area_pid);

               $area_id = explode(',', $areadata['child_ids']);

           } else if (!empty($area_pid) && !empty($area_cid)) {

               //如果有子id，查询子id

               $area_id = $area_cid;

           }
            $users = D('UserMember')->userList($teaching, $label, $sex, $age, $identity, $area_id, $star, $addtime, $salary, $keywords, $limit,$limitrow);

            // echo M()->getLastSql();exit();

           //dump($users);exit();


            if (strlen($keywords) > 1) {

                //获取搜索关键词标识

                $k = mb_substr($keywords, -1, 1, 'utf8');

            }

            //拼接ajax返回

            $users = $this->showForeach($keywords,$k,$users, $teaching, $label);
            // echo M()->getLastSql();exit();

           // var_dump($users);            exit();

            foreach ($users as &$v) {

                $v['jiaoguo'] = teached($v['id']);

                $v['sheng'] = getParentArea($v['area_id']);

                $v['shi'] = getArea($v['area_id']);

                $v['logos'] = getimg($v['logo']);

                $v['teaching'] = $teaching;

                $v['kecheng'] = getkecheng($v['courses']);

                $v['jiaoguo'] = teached($v['id']);

            }

//            dump($users);die;

            $this ->ajaxReturn($users);

        }
       
        //地区id

        if (!empty($area_pid) && empty($area_cid)) {

            //如果只有父id，查询所有子id

            $areadata = D('Area')->find($area_pid);

            $area_id = explode(',', $areadata['child_ids']);

        } else if (!empty($area_pid) && !empty($area_cid)) {

            //如果有子id，查询子id

            $area_id = $area_cid;

        }

        if($_GET['limitrow']){

            $limitrow = $_GET['limitrow'];

        }

        //teaching默认值

        if (empty($teaching)) {

            $teaching = $_GET['teaching'] = 2;

        }

        // dump($_GET);die;

        //查找用户列表
        $nums  = D('UserMember')->userList($teaching, $label, $sex, $age, $identity, $area_id, $star, $addtime, $salary, $keywords,'','',1);

       // var_dump($salary);  exit();
        
        $this ->assign('nums',$nums);
       
        $users = D('UserMember')->userList($teaching, $label, $sex, $age, $identity, $area_id, $star, $addtime, $salary, $keywords, $limit,$limitrow);
         //dump($users);die;  //hjl 这里还没有取出课程
        // echo M() ->_Sql();die;
        // echo M() -> _sql();

        if (strlen($keywords) > 1) {

            //获取搜索关键词标识

            $k = mb_substr($keywords, -1, 1, 'utf8');

        }
        // dump($users);die;
        $users = $this->showForeach($keywords,$k,$users, $teaching, $label);

         //dump($users);die;

        if($salary == 1){
	        foreach ($users as $key => &$value) {
	        	$value['minsalary'] = (int)$value['salary'];
	        }
        }
        // echo M() ->_Sql();die;
        $id = $_SESSION['onethink_home']['uid'];

        $num = D('Message')->showNum($id);

        $this->assign('num', $num);

        //获取所有地区信息

        $areas = D('Area')->getAreas();

        //获取所有课程标签

        $labels = getIteam('label');

        //获取身份标签

        $identity = getIteam('identity');
        //dump($users);exit;
        $this->assign('users', $users);

        $this->assign('areas', $areas);

        $this->assign('identity', $identity);

        $this ->assign('datetime',time());

        //dump(identity);
        //dump($labels);exit;
        $this->assign('labels', $labels);
        $this->assign('cur_label',$label);
        $this->display();

    }



    /**
     * 封装foreach的数据
     */

    public function showForeach($keywords,$k,$users, $teaching, $label) {
//        echo "<pre>";

       // var_dump($users);exit();


        foreach ($users as &$v) {

            //如果是搜索技能

            if ($k == 'c') {

                //关键词高亮显示

                 $this->hight(mb_substr($keywords, 0, -1, 'utf8'), $v['nickname']);

            }

            $v['area_id2'] = getAreaId($v['area_id']);

            //查找是否实名认证

            $res = D('Auth')->find($v['id']);

            if (!empty($res)) {

                $v['passed'] = $res['passed'];

            }

            //查找自我介绍

            $result = D('Introduce')->detail($v['id'], $teaching);

            $v['inaword'] = $result['inaword'];

            //查找课程  //hjl  add $label

            $courses = D('Course')->userCourses($v['id'], $teaching,$label);
            //价格
            // foreach ($users as $key => $value) {
            // 	$value['minsalary'] = $value['salary'];
            // }
            foreach ($courses as $val) {

                //如果搜索课程

                $v['courses'][] = ($k == 'c' ? $this->hight(mb_substr($keywords, 0, -1, 'utf8'), $val['title']) : $val['title']);

            }

            //最多显示三门课程

            $v['courses'] = array_slice($v['courses'], 0, 3);

            //查找用户最低价格
            // dump($label);die;
            $res = D('Course')->minSalary($v['id'], $teaching, $label);

            //去掉整数后的.00

            $v['minsalary'] = ($res['minsalary'] == (int) $res['minsalary']) ? (int) $res['minsalary'] : $res['minsalary'];

        }

        unset($v);

        return $users;

    }



    /**
     * 关键词高亮显示
     * @param  [str] $key [关键词]
     * @param  [str] $str [匹配的字符串]
     * @return [str]      [替换后的结果]
     * @author qlj
     */

    public function hight($key, $str) {
        return preg_replace('/(?!<[^>]*)' . $key . '(?![^<]*>)/i', '<strong style="color:#cc0000;">' . $key . '</strong>', $str);
    }



    /**
     * 想教想学详情
     * @param  [type] $uid      用户id
     * @param  [type] $teaching 教or学，1学,2教
     * @author qlj
     */

    public function userDetail($uid = '', $teaching = '',$linum = '') {
           
        if (empty($teaching)) {

            $teaching = $_GET['teaching'] = 2;

        }

        $cur_label=$_GET['cur_label'];  //hjl:当前label
        
        $linum = $_GET['linum'];
		
        $is_zj = $_GET['is_zj']>0 ? 1 : 0;//lkl判断是否是自荐

        //个人信息

        $user = D('UserMember')->showUser($uid);
	foreach(getIteam('constell') as $constell){
		if($user['constell'] == $constell['value']){
			$user['constell'] = $constell['title'];
			break;
		}
	}

        //自我介绍

        $result = D('Introduce')->detail($uid, $teaching);

        $user['introduce'] = $result['introduce'];

        $user['area_id2'] = getAreaId($user['area_id']);

        //授课信息

        $courses = D('Course')->userCourses($uid, $teaching,$cur_label, '', 1);

        foreach ($courses as &$v) {

            //课程评论数

            $res = D('Course_eva')->sumEva($v['id']);

            $v['sum'] = $res['sum'];

            //去掉整数后的.00

            $v['salary'] = ($v['salary'] == (int) $v['salary']) ? (int) $v['salary'] : $v['salary'];

        }

        unset($v);

        //取相册最新4张图片

        // $imgs = D('Album')->limit4($uid);

        $imgs = D('Album')->showAll($uid);

        //查找是否已收藏

        if (!empty($uid)) {

            $result = D('Collect')->collectList(session('uid'), $uid, $teaching);

            $collected = empty($result);

            $this->assign('collected', $collected);

        }



        Vendor('Jsdk.jssdk');

        $appid = 'wx24af86636d874b65';

        $AppSecret = 'cf8e1c33e151589634b9f9d3a1c96c26';

        $Jsdk = new \JSSDK($appid, $AppSecret);

        $data = $Jsdk->getSignPackage();

        $this->assign('data', $data);



        //评价标签

        $eva = D('TeacherEva')->find($uid);

        if (!empty($eva['labels'])) {

            $eva['labels'] = explode(',', $eva['labels']);

        }


        if (!empty($_SERVER['HTTP_REFERER'])){

            $backurl = $_SERVER['HTTP_REFERER'];

                if (isset($_GET['linum'])){

                    $backurl = substr($backurl,0,strripos($backurl,'keywords/')+9) . '/limit/0/limitrow/' .$_GET['linum'];   

                }

            $this -> assign('backurl',$backurl);

        }



        //查找综合评价

        $getStar = D('TeacherEva')->getStar($uid);

        $this->assign('getStar', $getStar);
        // echo $_SESSION['onethink_home']['uid'],'******';
        // print_r($user);exit;
        $this->assign('user', $user);

        $this->assign('courses', $courses);

        $this->assign('uid', session('uid'));

        $this->assign('imgs', $imgs);

        $this->assign('eva', $eva);
		
        $this->assign('is_zj', $is_zj);//lkl判断是否是自荐

        $this->assign('is_vip', is_vip(session('uid')));

        $this->assign('pause', get_pause(session('uid')));

        $this->display();

    }



    /**
     * 咨询页面
     * @param  [int] $uid [用户id]
     * @author qlj
     */

    public function consult() {

        if (IS_AJAX) {
			
			/* lkl自荐不限制咨询次数 S*/
			$is_zj = $_GET['is_zj'] > 0 ? $_GET['is_zj'] : 0;
			
			if($is_zj == 1){
				$this->ajaxReturn('');
			}else{
				
				if ($this->seconds('checked')) {

					$this->ajaxReturn('');

				} else {
					$this->ajaxReturn('您今日查看联系方式的次数已用尽。');
				}
				/* lkl自荐不限制咨询次数 E*/
			}

        } else {

            $uid = I('get.uid');

            $info = D('UserMember')->showUser($uid);

            $this->assign('info', $info);

            $this->display();

        }

    }



    /**
     * 控制查看次数
     * @param  [str] $column [字段名，checked或messages]
     * @return [bool]
     * @author qlj
     */

    public function seconds($column) {
        $user = D('UserMember')->showUser(session('uid'));
        //hjl  如果是第二天更新重置咨询=次数为0
       	 
		  $cur_d  =  intval(date('d',time()));      //当前日期
		  $last_d = intval(date('d',$user['last_zx_time']));  //用户最后一次修改的日期
		  $a = $cur_d-$last_d;
		  
         if($a >= 1){
        	$update['checked']=0;
        	$where['id']=session('uid');
            D('UserMember')->where($where)->save($update);
         }
        //hjl  end
        $user = D('UserMember')->showUser(session('uid'));

        //如果非会员且查看次数小于3，或者是会员查看次数小于10

        if (($user['vip_endtime'] < time() && $user[$column] < 3) || ($user['vip_endtime'] >= time() && $user[$column] < 10)) {
           
            $update['id'] = session('uid');
			
			/* lkl修改，点击提交后才更新用户点击次数 s*/
			if($column != 'messages'){
				$update[$column] = intval($user[$column]) + 1;
                $update['last_zx_time']=time();  //hjl:更新最后一次咨询时间
				//修改查看次数

				if (D('UserMember')->updatePost($update)) {
                    
					return true;

				}
			}
			
			return true;
			/* lkl修改，点击提交后才更新用户点击次数 e*/

        }
        
        return false;

    }



    public function erweima_up() {

        $id = I('get.id');

        if ($_FILES) {



            $logo = $_FILES;

            // dump($logo);

            // die;

            $arr['rootPath'] = "./Uploads/Picture/";

            $file = D('Picture')->upload($logo, $arr);

            if ($file) {

                // dump($file);die;

                $info = D('UserMember')->where(array('id' => $id))->setField('qr_code', $file['qr_code']['id']);

                if ($info) {

                    $this->success('上传成功', U('Index/index'));

                }

            } else {

                $this->error('请选择图片');

            }

        } else {

            $qr_code = getLogin($id, 'qr_code');

            $this->assign('pics', $qr_code);

            $this->display();

        }

    }



    /**
     * 发送短信验证吗
     */

    public function ajaxYzm() {

        if(IS_GET){

            $mobile = I('get.mobile');

            $result = $this ->getIsMobile($mobile);

           // echo M() ->_Sql();die;

        if($result){

            $this ->ajaxReturn($result);

        }

        die;

        }

        $flag = 0;

        $params = ''; //要post的数据

        $mobile = I('post.mobile'); //手机号
		$verify = rand(123456, 999999); //获取随机验证码
        $bj = I('get.bj');  //hjl:1 代表注册
		
		if($bj==1){
		    $mycontent='您的注册验证码为：' . $verify . '，10分钟内有效。';
		}else{
		    $mycontent='你的重置密码验证码：' . $verify . '，10分钟内有效。';	
		}
		
       
		

        //以下信息自己填以下

        $argv = array(

            'name' => '13811286300', //必填参数。用户账号

            'pwd' => 'DC062075695DF35084F0FDC4374A', //必填参数。（web平台：基本资料中的接口密码）

            'content' => $mycontent, //必填参数。发送内容（1-500 个汉字）UTF-8编码

            'mobile' => $mobile, //必填参数。手机号码。多个以英文逗号隔开

            'stime' => '', //可选参数。发送时间，填写时已填写的时间发送，不填时为当前时间发送

            'sign' => '全民教学公众平台', //必填参数。用户签名。

            'type' => 'pt', //必填参数。固定值 pt

            'extno' => ''    //可选参数，扩展码，用户定义扩展码，只能为数字

        );

        //print_r($argv);exit;

        //构造要post的字符串

        //echo $argv['content'];

        foreach ($argv as $key => $value) {

            if ($flag != 0) {

                $params .= "&";

                $flag = 1;

            }

            $params .= $key . "=";

            $params .= urlencode($value); // urlencode($value);

            $flag = 1;

        }

        $url = "http://web.duanxinwang.cc/asmx/smsservice.aspx?" . $params; //提交的url地址

        $con = substr(file_get_contents($url), 0, 1);  //获取信息发送后的状态

        session('verify', $verify);

        if ($con == '0') {

            D('Sms')->addSms($argv, 1);

            $this->ajaxReturn("发送成功!");

        } else {

            D('Sms')->addSms($argv, 2);

            $this->ajaxReturn("发送失败!'");

        }

    }



    /**
     * 忘记密码
     */

    public function forgetPwd() {
         //echo session('openid');exit;
        if (IS_POST) {

            $post = I('post.');
            //print_r($post);exit;
            $verify = session('verify');
            //hjl 临时屏蔽
            if ($verify != $post['con']) {

                $this->error('验证码错误');

                die;

            }

            if (D('UserMember')->forgetPwd($post)) {
                
            //hjl:跳转到用户中心 start
                            
                 $sql="select * from tutor_user_member where username='$post[mobile]'";

				 $data = M()->query($sql);
				 $uid=$data[0]['id'];
                 //echo $uid;exit;
                  D('UserMember')->is_login($post['mobile']);
                if($uid){
                   session('uid', $uid);

                  //更改登录后的状态
                  //session('is_login', 0);
                   D('UserMember')->updateLogin('mobile', $post['mobile'], 1);
                    //var_dump(empty($data[0]['openid']));exit;
                   if (empty($data[0]['openid'])) {

                    // if(!empty(session('uid'))){
                    //     $this->logout();
                    //     die;
                    // }
                       $this->codes();
                        file_put_contents('bbb.txt',var_export(session('openid'),true),FILE_APPEND);  
                       D('UserMember')->updateOpenid(session('uid'));
					
				
                  }
				  
				 
				
            }else {

                $this->error('登录失败', U('UserMember/register'));

            }
               

                //hjl end
             //记住密码
                $user = D('UserMember')->showUser(session('uid'));
                setcookie('username', $user['username'], time() + 3600 * 24 * 365);

                setcookie('password', $post['password'], time() + 3600 * 24 * 365);
                
                setcookie('remember', 1, time() + 3600 * 24 * 365);

                $this->success('密码修改成功',U('Index/index'));

            } else {

                $this->where('密码修改失败');

            }

        } else {

            $this->display('forgetpwd');

        }

    }



    //用户协议

    public function agreement() {

        $this->display();

    }



    //停止授课，学习

    public function endcourse($column) {

        if (IS_AJAX) {

            $info = D('UserMember')->showUser(session('uid'));

            if ($info[$column] == 0) {

                $data[$column] = 1;

            } else {

                $data[$column] = 0;

            }

            $data['id'] = session('uid');



            if (D('UserMember')->updatePost($data)) {

                $this->ajaxReturn('操作成功！');

            } else {

                $this->ajaxReturn('好像出错了，待会再试试吧');

            }

        }

    }



    //修改密码

    public function upPwd() {

        $password = I('post.password');

        $spassword = I('post.spassword');
        if(strlen($spassword) < 5){
            $this->error('密码不能小于6位数');
        }
        if(empty($spassword)){
        	$this->error('修改密码不能为空');
        }
        if($password == $spassword){
            $this->error('原密码不能相同');
        }
        // dump($password);dump($spassword);die;
        // if ($password != $spassword) {

        //     $this->error('俩次输入有误');

        //     die;

        // }

        $id = session('uid');
        $password = D('UserMember')->getMd5($id, $password, 1);
        $spassword = D('UserMember')->getMd5($id, $spassword, 1);

        if (D('UserMember')->updatePassword($password,$spassword)) {

            $this->success('修改成功');

        } else {

            $this->success('原密码错误');

        }

    }



    public function jubao() {

        $id = I('post.c_uid');

        if (D('UserMember')->jubao($id)) {

            $this->ajaxReturn('举报成功');

        } else {

            $this->ajaxReturn('举报失败');

        }

    }

    public function getIsMobile($mobile){

        $data = D('UserMember') ->getIsMobile($mobile);

        return $data;

    }

}

